﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TKD06
    {

        public TKD06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VKUNDVER();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                LIEFBED = "";
                ZAHLBED = "";
                STEUER = "";
            }
            else
            {
                KEY = TMV.KDVKUNDNR + TMV.KDVVB + TMV.KDVVG;
                ZAHLBED = TMV.KDVZAHLBED;
                STEUER = TMV.KDVSTEUER;
                LIEFBED = TMV.KDVLIEFBED;
            }
        }
        public override void LOADBYAUF()
        { 
            base.LOADBYAUF();
            TBMain.Clear();
            if (ORDERNUM.Length < 14)
                return;
            dwData = TBMain.NewRow();
            dwData["KDVMANDANT"] = app.Mandant;
            dwData["KDVKUNDNR"] = ORDERNUM.Substring(0, 8);
            dwData["KDVVB"] = ORDERNUM.Substring(8, 3);
            dwData["KDVVG"] = ORDERNUM.Substring(11, 3);
            SResult = TMV.QKDV01(dwData);
            MergeData(SResult, true);
            if (!IsAuto)
                return;
            if (TBMain.Rows.Count > 0)
                return;
           
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["KDVMANDANT"] = app.Mandant;
            dwData["KDVKUNDNR"] = ORDERNUM.Substring(0, 8);
            dwData["KDVVB"] = ORDERNUM.Substring(8, 3);
            dwData["KDVVG"] = ORDERNUM.Substring(11, 3);
            TMV.InsertKUNDVER(dwData, app.company);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void OnSave()
        {
            if (dwMaster == null)
                return;
            if (!IsSaved)
                return;
            TMV.UpdateKUNDVER(dwMaster, 0);
            string key = KEY;
            KEY = "";
            KEY = key;            
            base.OnSave();
        }
    }
}
